<?php
class shopModel extends model{
	public $base;
	public function __construct(&$base){
		parent::__construct($base);
		$this->base=$base;
		$this->table="shop";
	}
	
	public function tplcat(){
		return array(
			"base"=>"基础",
			"vip"=>"Vip版",
			"buy"=>"合作版",
		);
	}
	
	public function tpllist(){
		return array(
			"base"=>"index/",
			"vip"=>"vip/",
			"buy"=>"shop/",
		);
	}
	
	public function favlist(){
		 
		if(M("login")->userid){
			$shopids=M("fav")->selectCols(array(
				"where"=>" tablename='shop' AND userid=".M("login")->userid,
				"fields"=>"object_id"
			));
			$shops=$this->getShopByIds($shopids);
			
			return $shops;
		}else{
			return false;
		}
	}
	
	
	function recommend($limit=8){
		$sc_id=M("site_city")->sc_id();
		$where=" siteid=".SITEID." AND status=2 AND isrecommend=1 ";
		 
		if($sc_id){
			//$where.=" AND sc_id=".$sc_id;
		}
		$option=array(
			"where"=>$where,
			"limit"=>$limit,
			"order"=>" rand() "		
		);
		return $this->shoplist($option);
	}
	
	public function shoplist($option=array()){
		$sc_id=M("site_city")->sc_id();
		if(!isset($option['where'])){
			$where=" siteid=".SITEID." AND status=2";
			if($sc_id){
				$where.=" AND sc_id=".$sc_id;
			}
			$option['where']=$where;
		}
		if(isset($_SESSION['ss_latlng']))
		{				
			$latlng=explode("-",$_SESSION['ss_latlng']);
			$lat=$latlng[0];
			$lng=$latlng[1];
							 
		}elseif($_COOKIE['ck_latlng']){
			$latlng=explode("-",$_COOKIE['ck_latlng']);
			$lat=$latlng[0];
			$lng=$latlng[1];
		}
		
		$data=$this->select($option);
		if($data){
			foreach($data as $k=>$v){
				$v['distance']=distanceByLnglat($lng,$lat,$v['lng'],$v['lat']);
	 			$v['month_buy']=$this->month_buy($v['shopid']);
			    $data[$k]=$v;
			}
		}
		return $data;
	}
	
	public function month_buy($shopid){
		return M("shop_order")->selectOne(array(
			"where"=>" shopid=".$shopid." AND status=3 ",
			"fields"=>"count(*)"
		));
		
	}
	
	public function getShopByIds($shopids,$fields=""){
		if(!empty($shopids)){
			$option['where']=" shopid in(".implode(",",$shopids).")";
			$fields && $option['fields']=$fields;
			$data=$this->shoplist($option);
			if($data){
				$shoplist=array();
				foreach($data as $k=>$v){
					$shoplist[$v['shopid']]=$v;
				}
				return $shoplist;
			}
		}
		return false;
	} 
	
	public function is_invite(){
		return array(
			1=>"邀请成功",
			2=>"未邀请",
			3=>"邀请失败",
		);
	}
	public function working($time){
		$t_d=explode("-",$time);
		$t_s=explode(":",$t_d[0]);
		$t_e=explode(":",$t_d[1]);			
		$type=opentype($t_s[0],$t_s[1],$t_e[0],$t_e[1]);
		return $type;
	}
	

	
	public function id_shop($option){
		$d=$this->select($option);
		if($d){
			foreach($d as $v){
				$data[$v['shopid']]=$v;
			}
			return $data;
		}
	}
	public function get($id){
		$id=intval($id);
		$data=$this->selectRow(array("where"=>"shopid=".$id));
		if(empty($data)) return false;
		if(empty($data['tpl'])){
			$data['tpl']="index/";
		}else{
			$data['tpl']=$data['tpl']."/";
		}
		return $data;		
	}
	
	
	public function addMoney($option){
		$shopid=$option['shopid'];
		 
		$row=$this->selectRow("shopid=".$shopid);
		if(empty($row)) return false;
		$data=array();
		if(isset($option['money'])){
			$data['money']=$row['money']+$option['money'];
			
		}
		
		if(isset($option['income'])){
			$data['income']=$row['income']+$option['income'];
		}
		
		if(isset($option['balance'])){
			$data['balance']=$row['balance']+$option['balance'];
		}
		$this->update($data,"shopid=".$row['shopid']);
		 
		$logdata=array(
				"dateline"=>time(),
				"shopid"=>$shopid,
				"type_id"=>1,
				"siteid"=>SITEID,				 
		);
		
		
		if(isset($option['balance'])){
			$logdata['k']='balance';
			$logdata['money']=$option['balance'];
			$logdata['content']=str_replace("[money]",$option['balance'],$option['content']) ." 原来".$row['balance']."元，现在".$data['balance']."元";
			M("shop_pay_log")->insert($logdata);
		}
		
		if(isset($option['income'])){
			$logdata['k']='income';
			$logdata['money']=$option['income'];
			$logdata['content']=str_replace("[money]",$option['income'],$option['content'])." 原来".$row['income']."元，现在".$data['income']."元";
			M("shop_pay_log")->insert($logdata);
		}
		
		if(isset($option['money'])){
			$logdata['k']='money';
			$logdata['money']=$option['money'];
			$logdata['content']=str_replace("[money]",$option['money'],$option['content'])." 原来".$row['money']."元，现在".$data['money']."元";
			M("shop_pay_log")->insert($logdata);
		}
		
	}
	
}

?>